/**
 * 订单管理 主机列表
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>主机管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.host_ip" placeholder="输入主机IP"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input size="small" v-model="formInline.host_name" placeholder="输入主机名称"></el-input>
      </el-form-item>
<!--      <el-form-item>-->
<!--        <el-input size="small" v-model="formInline.transId" placeholder="输入交易单号"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-select size="small" v-model="formInline.payType" placeholder="请选择">
          <el-option v-for="type in payType" :label="type.tag" :value="type.id" :key="type.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select size="small" v-model="formInline.orderStatus" placeholder="请选择">
          <el-option v-for="type in payway" :label="type.source" :value="type.id" :key="type.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>

      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="index" width="60">
      </el-table-column>
      <el-table-column sortable prop="host_ip" label="主机IP" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="host_name" label="主机名称" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="host_tag_name" label="主机标签" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="host_source_name" label="主机来源" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="host_OS" label="主机版本信息" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="host_CPU" label="CPU核数" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="host_RAM" label="内存值" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="host_disk" label="硬盘总空间" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="host_status_name" label="主机状态" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="is_manage_name" label="管理状态" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="add_date" label="创建时间" width="180" show-overflow-tooltip>
        <template slot-scope="scope">
<!--          <div>{{scope.row.add_date|timestampToTime}}</div>-->
          <div>{{scope.row.add_date}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="50%" @click="closeDialog('editForm')">
      <el-form label-width="120px" :model="editForm" ref="editForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="主机IP">
              <el-input size="small" v-model="editForm.host_ip" auto-complete="off" placeholder="请输入主机IP"></el-input>
            </el-form-item>
<!--            <el-form-item label="主机标签">-->
<!--              <el-input size="small" v-model="editForm.host_tag_name" auto-complete="off" placeholder="请输入主机标签"></el-input>-->
<!--            </el-form-item>-->
            <el-form-item label="主机标签">
              <el-select size="small" v-model="editForm.host_tag" placeholder="请选择">
                <el-option v-for="type in payType" :label="type.tag" :value="type.id" :key="type.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="主机版本">
              <el-input size="small" v-model="editForm.host_OS" auto-complete="off" placeholder="请输入主机版本"></el-input>
            </el-form-item>
            <el-form-item label="内存值">
              <el-input size="small" v-model="editForm.host_RAM" auto-complete="off" placeholder="请输入内存值"></el-input>
            </el-form-item>
<!--            <el-form-item label="主机状态">-->
<!--              <el-input size="small" v-model="editForm.host_status_name" auto-complete="off" placeholder="请输入主机状态"></el-input>-->
<!--            </el-form-item>-->
            <el-form-item label="主机状态">
<!--              <el-select size="small" v-model="formInline.hostStatus" placeholder="请选择">-->
              <el-select size="small" v-model="editForm.host_status" placeholder="请选择">
                <el-option v-for="type in hostStatus" :label="type.title" :value="type.id" :key="type.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机名称">
              <el-input size="small" v-model="editForm.host_name" auto-complete="off" placeholder="请输入主机名称"></el-input>
            </el-form-item>
<!--            <el-form-item label="主机来源">-->
<!--              <el-input size="small" v-model="editForm.host_source_name" auto-complete="off" placeholder="请输入主机来源"></el-input>-->
<!--            </el-form-item>-->
                  <el-form-item label="主机来源">
                    <el-select size="small" v-model="editForm.host_source" placeholder="请选择">
                      <el-option v-for="type in payway" :label="type.source" :value="type.id" :key="type.id"></el-option>
                    </el-select>
                  </el-form-item>
            <el-form-item label="CPU核数">
              <el-input size="small" v-model="editForm.host_CPU" auto-complete="off" placeholder="请输入CPU核数"></el-input>
            </el-form-item>
            <el-form-item label="硬盘总空间">
              <el-input size="small" v-model="editForm.host_disk" auto-complete="off" placeholder="请输入硬盘总空间"></el-input>
            </el-form-item>
<!--            <el-form-item label="管理状态">-->
<!--              <el-input size="small" v-model="editForm.is_manage_name" auto-complete="off" placeholder="请输入管理状态"></el-input>-->
<!--            </el-form-item>-->
            <el-form-item label="管理状态">
              <el-select size="small" v-model="editForm.is_manage" placeholder="请选择">
                <el-option v-for="type in IsManage" :label="type.title" :value="type.id" :key="type.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-form-item label="添加时间">-->
<!--          <el-input size="small" v-model="editForm.add_date" auto-complete="off" placeholder="请输入微信证书路径"></el-input>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { OrderList, OrderListPage, OrderDelete, OrderSave,OrderAdd, HostTagList, HostSourceList, HostSearch} from '../../api/payMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '修改',
      update_add: null,
      payType: [
        { tag: '请选择', id: 0 },
        { tag: '券商1', id: 1 },
        { tag: '券商2', id: 2 },
        { tag: '券商3', id: 3 },
        { tag: '券商4', id: 4 },
        { tag: '券商5', id: 5 },
        { tag: '券商6', id: 6 },
      ],
      payway: [
        { source: '请选择', id: 0 },
        { source: '阿里云', id: 1 },
        { source: '腾讯云', id: 2 },
      ],
      hostStatusName:{
        0: '正常',
        1: '异常',
      },
      IsManageName:{
        0: '已管理',
        1: '未管理',
      },
      hostStatus:[
        { title: '正常', id: 0 },
        { title: '异常', id: 1 },
      ],
      IsManage:[
        { title: '已管理', id: 0 },
        { title: '未管理', id: 1 },
      ],
      editForm: {
        id: '',
        host_ip: '',
        host_name: '',
        host_tag_name: '',
        host_source_name: '',
        host_OS: '',
        host_CPU: '',
        host_RAM: '',
        host_disk: '',
        host_status_name: '',
        is_manage_name: '',
        add_date: '',
        token: localStorage.getItem('logintoken')
      },
      formInline: {
        page: 1,
        limit: 10,
        host_ip: '',
        host_name: '',
        transId: '',
        payType: 0,
        orderStatus: 0,
        token: localStorage.getItem('logintoken')
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取公司列表
    getdata(parameter) {
      this.loading = true
      // 模拟数据开始
      // let res = {
      //   code: 0,
      //   msg: null,
      //   count: 23,
      //   data: [
      //     {
      //       host_ip: '170.2.5.5',
      //       host_name: 'test1',
      //       host_tag: '券商1',
      //       host_source: '阿里云',
      //       host_OS: 'centos',
      //       host_CPU: 6,
      //       host_RAM: 40,
      //       host_disk: 500,
      //       host_status: 0,
      //       is_manage: 0,
      //       add_date: 1526380193000,
      //     },
      //     {
      //       host_ip: '170.2.5.55',
      //       host_name: 'test2',
      //       host_tag: '券商3',
      //       host_source: '腾讯云',
      //       host_OS: 'windows',
      //       host_CPU: 6,
      //       host_RAM: 40,
      //       host_disk: 500,
      //       host_status: 0,
      //       is_manage: 0,
      //       add_date: 1526380176000
      //     },
      //     {
      //       host_ip: '170.25.5.55',
      //       host_name: 'test3',
      //       host_tag: '券商2',
      //       host_source: '华为云',
      //       host_OS: 'centos',
      //       host_CPU: 4,
      //       host_RAM: 20,
      //       host_disk: 1024,
      //       host_status: 0,
      //       is_manage: 0,
      //       add_date: 1524921444000,
      //     }
      //   ]
      // }
      // this.loading = false
      // this.listData = res.data
      // this.pageparm.currentPage = this.formInline.page
      // this.pageparm.pageSize = this.formInline.limit
      // this.pageparm.total = res.count
      // 模拟数据结束

      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      OrderList(parameter)
        .then(res => {
          this.loading = false
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.listData = res.data.results
            // 分页赋值
            this.pageparm.currentPage = this.formInline.page
            this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = res.data.count
            OrderListPage(this.pageparm).then(res => {
              this.listData = res.data.results
            })
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('菜单加载失败，请稍后再试！')
        })

      HostTagList(parameter)
        .then(res => {
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.payType = res.data
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('菜单加载失败，请稍后再试！')
        })

      HostSourceList(parameter)
        .then(res => {
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.payway = res.data
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('菜单加载失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {

    },
    //显示编辑界面
    // handleEdit: function(index, row) {
    //   this.editFormVisible = true
    //   this.editForm = row
    // },
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.update_add = true
        this.editForm.id = row.id
        this.editForm.host_ip = row.host_ip
        this.editForm.host_name = row.host_name
        this.editForm.host_tag_name = row.host_tag_name
        this.editForm.host_source_name = row.host_source_name
        this.editForm.host_OS = row.host_OS
        this.editForm.host_CPU = row.host_CPU
        this.editForm.host_RAM = row.host_RAM
        this.editForm.host_disk = row.host_disk
        this.editForm.host_status_name = row.host_status_name
        this.editForm.is_manage_name = row.is_manage_name
        this.editForm.add_date = row.add_date
      } else {
        this.title = '添加'
        this.update_add = false
        this.id = ''
        this.editForm.host_ip = ''
        this.editForm.host_name = ''
        this.editForm.host_tag_name = ''
        this.editForm.host_source_name = ''
        this.editForm.host_OS = ''
        this.editForm.host_CPU = ''
        this.editForm.host_RAM = ''
        this.editForm.host_disk = ''
        this.editForm.host_status_name = ''
        this.editForm.is_manage = ''
        this.editForm.add_date = ''
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (this.update_add) {
          OrderSave(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '公司保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('支付配置信息保存失败，请稍后再试！')
            })
        } else {
          OrderAdd(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '主机新增成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('支付配置信息保存失败，请稍后再试！')
            })
        }
      })
    },
    // 删除公司
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          OrderDelete(row.id)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '主机已删除!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('支付配置信息删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog(formName) {
      this.editFormVisible = false
      // this.$refs[formName].resetFields()
    }
  },

  computed: {
    selectParams() { // 当下面的属性变化时，本计算属性会重新计算，得到一个params对象，用于后续传递给axios
      return {
        host_ip__icontains: this.formInline.host_ip || '',
        host_name__icontains: this.formInline.host_name || '',
        payType__icontains: this.formInline.payType || '',
        orderStatus__icontains: this.formInline.orderStatus || '',
      };
    },
  },

  watch: {
    selectParams() { // 监听计算属性，当计算属性发生变化向后端发送请求
      HostSearch(this.selectParams).then(res => {
        this.listData = res.data.results
        this.listLoading = false
      })
    }
  },
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>




